home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3
/
CHAPTE19
/
EX5.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-03
|
4KB
|
122 lines
#include <windows.h>
#include <winreg.h>
#include <genstub.c>
// Menu options.
#define IDM_SAVE 400 // Create a Hive.
#define IDM_RESTORE 401 // Restore the Hive. Not supported in Win 95.
#define IDM_LOAD 402 // Load the Hive.
#define IDM_UNLOAD 403 // Unload the Hive.
LRESULT FAR PASCAL WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch ( uMsg )
{
case WM_COMMAND:
{
char szErrMsg[128]; // Buffer for message to print in message boxes
HKEY hKey; // Handle of key.
LRESULT lResult; // Tests function return values.
switch ( wParam )
{
case IDM_SAVE:
{
DWORD dwDisp;
// Build the contents of the KeyRoot and its subkeys.
RegCreateKeyEx( HKEY_CLASSES_ROOT, "KeyRoot\\Test\\Test1", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp );
RegSetValueEx( hKey, "Value1", 0, REG_SZ, "ABC", 14 );
RegSetValueEx( hKey, "Value2", 0, REG_SZ, "BCD", 14 );
RegCloseKey( hKey );
RegCreateKeyEx( HKEY_CLASSES_ROOT, "KeyRoot\\Test\\Test2", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwDisp );
RegSetValueEx( hKey, "Value1", 0, REG_SZ, "ZYX", 14 );
RegCloseKey( hKey );
// Save key, "KeyRoot", in hive, "TheHive".
RegOpenKeyEx( HKEY_CLASSES_ROOT, "KeyRoot", 0, KEY_ALL_ACCESS, &hKey );
RegSaveKey( hKey, "TheHive", NULL );
RegCloseKey( hKey );
}
break;
case IDM_LOAD:
{
lResult = RegLoadKey( HKEY_LOCAL_MACHINE, "KeyRoot", "TheHive" );
if ( GetLastError() == ERROR_SUCCESS )
MessageBox( hWnd, "TheHive was loaded into HKEY_LOCAL_MACHINE",
"MESSAGE", MB_OK );
else {
wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
}
}
break;
case IDM_UNLOAD:
{
lResult = RegUnLoadKey( HKEY_LOCAL_MACHINE, "KeyRoot" );
if ( GetLastError() == ERROR_SUCCESS )
MessageBox( hWnd, "TheHive Was Unloaded",
"MESSAGE", MB_OK );
else {
wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
}
}
break;
case IDM_RESTORE:
{
DWORD dwDisposition = 0;
RegCreateKeyEx( HKEY_LOCAL_MACHINE, "KeyRoot", 0, "",
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
NULL, &hKey, &dwDisposition );
RegRestoreKey( hKey, "TheHive", 0 );
if ( GetLastError() != ERROR_SUCCESS )
{
wsprintf( szErrMsg, "Error %d occurred", GetLastError( ) );
MessageBox( hWnd, szErrMsg, "ERROR", MB_OK );
RegDeleteKey( HKEY_LOCAL_MACHINE, hKey );
}
else
MessageBox( hWnd, "The Hive was restored", "MESSAGE", MB_OK );
RegCloseKey( hKey );
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
}
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default: // default windows message processing
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return( 0L );
}